﻿using Domain.Roles.Events;
using Domain.Roles.Exceptions;
using Domain.Roles.Interfaces;
using Domain.Roles.ValueObjects;
using Shared.Domain.Abstructs;
using Shared.Domain.Interface;

namespace Domain.Roles.Entities
{
    public partial class Role : BaseEntity<Guid>, IAggregateRoot
    {
        /// <summary>
        /// 更改启用状态
        /// </summary>
        /// <param name="enable"></param>
        public void UpdateEnable(bool enable)
        {
            bool oldValue = this.Enable;
            this.Enable = enable;
            _domainEvents.Add(new RoleEnableUpdated(Id, oldValue, enable));
        }

        /// <summary>
        /// 权限更改
        /// </summary>
        /// <param name="permissionSigns"></param>
        public void UpdatePermossion(IList<string> permissionSigns, IPermissionRegistry verifier)
        {
            foreach (var item in permissionSigns)
            {
                if (!verifier.IsExist(item))
                    throw new InvalidRolePermissionException($"权限{item}不存在");
            }

            var oldSign = this._permissionSigns;
            this._permissionSigns = permissionSigns;

            _domainEvents.Add(new RolePermissionUpdated(Id, oldSign.AsReadOnly(), permissionSigns.AsReadOnly()));
        }

        /// <summary>
        /// 更改角色名
        /// </summary>
        /// <param name="roleName"></param>
        public void UpdateName(string roleName)
        {
            RoleName newName = RoleName.Create(roleName);
            string oldName = this.RoleName;
            this.RoleName = newName;
            _domainEvents.Add(new RoleNameUpdated(Id, oldName, newName));
        }

        /// <summary>
        /// 更改角色描述
        /// </summary>
        /// <param name="description"></param>
        public void UpdateDescription(string description)
        {
            //string oldDescription = this.Description ?? string.Empty;
            this.Description = description;
            //_domainEvents.Add(new )
        }
    }
}
